﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Demos
{
    public partial class Demo0004 : UserControl
    {
        private Storyboard storyboard = new Storyboard();
		private Storyboard storyboard2 = new Storyboard();
		private DoubleAnimation horizontalAnimation = new DoubleAnimation();
		private DoubleAnimation verticalAnimation = new DoubleAnimation();
		private DoubleAnimation scaleAnimationX = new DoubleAnimation();
		private DoubleAnimation scaleAnimationY = new DoubleAnimation();
        private static double SCALE = 0.9;
		private static double SPRINGINESS = 0.5;
		private static int ROTATION_FACTOR = 32;
        private Point? downPosition = default(Point?);
        public Demo0004()
        {
            InitializeComponent();
            border.Projection = new PlaneProjection();
			border.RenderTransform=new ScaleTransform();
			Storyboard.SetTargetProperty(this.horizontalAnimation, new PropertyPath("RotationX", new object[0]));
			Storyboard.SetTargetProperty(this.verticalAnimation, new PropertyPath("RotationY", new object[0]));
			Storyboard.SetTargetProperty(this.scaleAnimationX, new PropertyPath("ScaleX", new object[0]));
			Storyboard.SetTargetProperty(this.scaleAnimationY, new PropertyPath("ScaleY", new object[0]));
			Storyboard.SetTarget(this.horizontalAnimation, border.Projection);
			Storyboard.SetTarget(this.verticalAnimation,border.Projection);
			Storyboard.SetTarget(this.scaleAnimationX,border.RenderTransform);
			Storyboard.SetTarget(this.scaleAnimationY, border.RenderTransform);
			ElasticEase elasticEase = new ElasticEase();
			elasticEase.Oscillations=1;
			elasticEase.Springiness=SPRINGINESS;
			elasticEase.EasingMode=0;
			this.horizontalAnimation.EasingFunction=elasticEase;
			this.horizontalAnimation.Duration= new Duration(TimeSpan.FromMilliseconds(1500.0));
			ElasticEase elasticEase2 = new ElasticEase();
			elasticEase2.Oscillations=1;
			elasticEase2.Springiness=SPRINGINESS;
			elasticEase2.EasingMode=0;
			this.verticalAnimation.EasingFunction=elasticEase2;
			this.verticalAnimation.Duration=new Duration(TimeSpan.FromMilliseconds(1500.0));
			this.scaleAnimationX.Duration=new Duration(TimeSpan.FromMilliseconds(100.0));
			this.scaleAnimationY.Duration=new Duration(TimeSpan.FromMilliseconds(100.0));
			this.storyboard.Children.Add(this.horizontalAnimation);
			this.storyboard.Children.Add(this.verticalAnimation);
			this.storyboard2.Children.Add(this.scaleAnimationX);
			this.storyboard2.Children.Add(this.scaleAnimationY);

            border.MouseLeftButtonDown += border_MouseLeftButtonDown;
            border.MouseLeftButtonUp += border_MouseLeftButtonUp;
            border.MouseMove += border_MouseMove;
            border.MouseEnter += border_MouseEnter;
        }

        void border_MouseEnter(object sender, MouseEventArgs e)
        {
            
			if (this.storyboard.GetCurrentState() == null)
			{
				return;
			}
			this.storyboard.Pause();
			Point position = e.GetPosition(this);
			double num = border.ActualWidth * 0.5;
			double num2 = border.ActualHeight * 0.5;
			this.verticalAnimation.From=(new double?(-(position.X - num) / num * (double)ROTATION_FACTOR));
			this.verticalAnimation.To=(new double?(0.0));
			this.horizontalAnimation.From=(new double?((position.Y - num2) / num2 * (double)ROTATION_FACTOR));
			this.horizontalAnimation.To=(new double?(0.0));
			this.storyboard.Begin();
        }

        void border_MouseMove(object sender, MouseEventArgs e)
        {
            
			if (this.downPosition.HasValue)
			{
				Point position = e.GetPosition(this);
				double num = Math.Max(Math.Abs(this.downPosition.Value.X - position.X)
                    , Math.Abs(this.downPosition.Value.Y - position.Y));
				if (num > 10.0)
				{
					this.downPosition = default(Point?);
					this.storyboard2.Pause();
					this.scaleAnimationX.To=(new double?(1.0));
					this.scaleAnimationY.To=(new double?(1.0));
					this.storyboard2.Begin();
				}
			}
        }

        void border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
			if (this.downPosition.HasValue)
			{
                this.storyboard2.Completed += OnClickStoryboardCompleted;
				this.storyboard2.Pause();
				this.scaleAnimationX.From =(new double?(SCALE));
				this.scaleAnimationX.To=(new double?(1.0));
				this.scaleAnimationY.From=(new double?(SCALE));
				this.scaleAnimationY.To=(new double?(1.0));
				this.storyboard2.Begin();
			}
        }
        
		private void OnClickStoryboardCompleted(object sender, EventArgs e)
		{
			this.storyboard2.Completed-=OnClickStoryboardCompleted;
            //if (this.Click != null)
            //{
            //    this.Click.Invoke(this, new EventArgs());
            //}
		}

        void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.downPosition = new Point?(e.GetPosition(this));
			(border.RenderTransform as ScaleTransform).CenterX= border.ActualWidth * 0.5;
			(border.RenderTransform as ScaleTransform).CenterY=border.ActualHeight*0.5;
			this.storyboard2.Pause();
			this.scaleAnimationX.From=new double?(1.0);
			this.scaleAnimationX.To=new double?(SCALE);
			this.scaleAnimationY.From=new double?(1.0);
			this.scaleAnimationY.To=new double?(SCALE);
			this.storyboard2.Begin();
        }


        private void OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                Application.Current.Resources.Remove("TileBrush");
                Application.Current.Resources.Add("TileBrush",new SolidColorBrush(Colors.Green));
            }
            catch (Exception ex)
            {
                this.ShowMessage(ex.Message,ex.ToString());
            }
        }
    }
}
